
// 数字和字母
const arr = [
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
]

/**
 * 获取几位的随机字符串
 * 
 * first：是否要求首位不以数字开头
 */
const getRandomStr = (number = 6, first = true) => {
	const value = (isFirst) => {
		return parseInt(Math.random() * arr.length - (isFirst ? 10 : 0))
	}
	let str = ''
	for (let i = 0; i < number; i++) {
		str += arr[value(i === 0)]
	}
	return str
}

/**
 * 获取签名
 * 
 * 一般由appsecret、timestamp和六位随机字符串再取md5决定
 */
const getSignature = (appsecret, timestamp = Date.now(), randomStr) => {
	if (appsecret) {
		if (!randomStr) {
			randomStr = getRandomStr(6)
		}
		const {secret, jsmd5} = require('./')
		var res = secret.SHA1(appsecret + timestamp + randomStr)
		res = jsmd5(res)
		return res
	}
	return ''
}

module.exports = {
	getRandomStr, getSignature
}

